<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'eleontev/Util/CryptEmail.php';


class KBClientView_comments extends KBClientView_common
{

	function &execute(&$manager) {
		
		$row = &$manager->getEntryById($this->entry_id, $this->category_id);
		$row = &$this->stripVars($row);
		if(!$row) { return; }
		
		$this->meta_title = $this->msg['comments_msg'];
		
		$s = $this->controller->getSetting('show_title_nav');
		$title = ($s) ? $row['title'] : '';
		
		$data = $this->getTop($manager, $title, false);
		$data .= $this->getCommentList($manager);
		$data .= $this->getForm($manager);
		$data .= $this->getEntry($manager, $row);
		
		return $data;		
	}
	
	
	function getCommentList(&$manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'comment_list.html');
		
		$bp = $this->pageByPage(20, $manager->getCommentListCountSql($this->entry_id));
		$by_page = $bp->shortNavigate();
		$tpl->tplAssign('page_by_page', $by_page);
		
		if($bp->num_pages > 1) {
			$tpl->tplSetNeeded('/by_page_bottom');
		}
		
		$rows = &$this->stripVars($manager->getCommentList($this->entry_id, $bp->limit, $bp->offset));
		if(!$rows) { return; }
		
		
		$parser = &KBCommentView_helper::getBBCodeObj();
		
		foreach($rows as $k => $row) {
			
			if($row['email']) {
				$tpl->tplSetNeeded('row/if_email');
				$tpl->tplAssign('mailto_link', CryptEmail::getMailto($row['email']));
			} elseif($row['name']) {
				$tpl->tplSetNeeded('row/if_name');
			}
			
			$tpl->tplAssign('comment', $parser->qparse(nl2br($row['comment'])));		
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplParse($row, 'row');
		}
		
		
		$tpl->tplAssign('js_script', CryptEmail::getMailtolJs(false));
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}	
	
 
	function getForm($manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'comment_form.html');
		
		$tpl->tplSetNeeded('/cancel_btn');
		
		$tpl->tplAssign('action_link', $this->_getLink('all'));
		$tpl->tplAssign('cancel_link', $this->_getLink('entry', $this->category_id, $this->entry_id));															  
		
		$tpl->tplAssign('user_msg', $this->getErrors());
		
		if(!$manager->is_registered) {
			$tpl->tplSetNeeded('/not_registered');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($this->getFormData());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function validate($values) {
		
		require_once 'eleontev/Validator.php';
		
		$v =& new Validator($values, true);
		$v->requiredArray('required_msg', array('comment'));
		
		if(isset($values['email'])) {
			$v->regex('email_msg', 'email', 'email', false);
		}
				
		return $v->getErrors();
	}
}
?>